/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-07-30 20:05:27
 * @LastEditTime: 2020-03-03 21:26:48
 * @LastEditors: Please set LastEditors
 */
var FLD = function() {
	return {

		serverIP: '39.105.222.87:20000',

		shoppingMallServerIP: '39.105.222.87:20002',
		version: window.localStorage.getItem('Version') || 0,
		ajax(cmd, param, callback, errorCallback) {
			// if (api.connectionType == 'none') {
			// 	if (typeof(layer) != 'undefined') {
			// 		layer.open({
			// 			content: '当前网络不可用,请连接网络!',
			// 			skin: 'msg',
			// 			time: 2
			// 		});
			// 	}
			// 	return false;
			// }

			// plus.device.getOAID({
			// 	success: function(res) {
			// 		console.log(JSON.stringify(res.oaid))

			var url = 'http://' + FLD.serverIP + '/Frame/SqlData?cmdname=' + cmd + '&platform=' + FLD.isMobile()
			// +'【' + 'a3d8db12-a93c-4e69-8ebe-34ec1fe1b3e9' + '】';
			// '【' + res.oaid + '】';


			if (FLD.getUserInfo()) {
				if (!param.hasOwnProperty("h_userId")) {
					param.h_userId = FLD.getUserInfo().UserId;
				}
			}
			// console.log(JSON.stringify(url))
			// console.log(JSON.stringify(param))
			$.ajax({
				headers: {
					'Version': FLD.version,
					'PackageName': 'com.fldsc.app'
				},
				url: url,
				async: true,
				data: param,
				type: "post",
				dataType: 'json',
				success: function(ret) {
					// alert(JSON.stringify(ret));
					if (ret) {
						if (ret.status == 200) {
							if (callback) callback(ret.data);
						} else {
							if (vm) {
								alert(url + '\n' + FLD.serverIP + '\n' + '错误代码1:' +
									'\n' + JSON
									.stringify(ret));
							}
						}
					} else {
						if (errorCallback) {
							errorCallback(err);
						} else {
							if (vm) {
								alert(url + '\n' + FLD.serverIP + '\n' + '错误代码2:' +
									'\n' + JSON
									.stringify(err));
							}

							// pingIP(FLD.serverIP1);
							// pingIP(FLD.serverIP2);
						}
					}
				},
				error: function(err) {
					console.log(url)
					console.log(JSON.stringify(err))
				}
			});

			// 	},
			// 	fail: function(e) {
			// 		console.log('getOAID failed: ' + JSON.stringify(e));
			// 	}
			// });



		},
		isMobile() {
			let reg = /iPhone|iPad|iPod|iOS|Macintosh/i;
			if (/android/i.test(navigator.userAgent.toLowerCase())) {
				return 'android'
			} else if (reg.test(navigator.userAgent.toLowerCase())) {
				return 'ios'
			}
		},
		GetOnlyDevice() {
			plus.device.getOAID({
				success: function(res) {
					console.log(JSON.stringify(res))
					return res.oaid

				},
				fail: function(e) {
					console.log('getOAID failed: ' + JSON.stringify(e));
				}
			});
		},
		uploadFile_shop(fileSrc, callback) {
			var url = 'http://' + FLD.shoppingMallServerIP + '/Admin/UploadHandler.ashx?action=appupload';
			api.ajax({
				headers: {
					'Version': FLD.version,
					'PackageName': 'com.fldsc.app'
				},
				url: url,
				method: 'post',
				dataType: 'text',
				data: {
					files: {
						file: fileSrc
					}
				}
			}, function(ret, err) {
				if (ret) {
					if (callback) callback(ret);
				} else {

					vm.showLoading = false;
					vm.refreshLoading = false;
					vm.ifConfirm = false;
					vm.$toast('程序好像出错了,请稍后再试\n' + 'errcode:' + err.code);
				}
			});
		},

		nativeRequest_shop(cmd, param, callback) {
			var url = 'http://' + FLD.serverIP + '/Frame/ST1_FLD/ShippingHandler.aspx?cmd=' + cmd;
			if (FLD.getUserInfo()) {
				if (!param.hasOwnProperty("h_userId")) {
					param.h_userId = FLD.getUserInfo().UserId;
				}
			}

			$.ajax({
				url: url,
				async: true,
				data: param,
				type: "post",
				dataType: 'json',
				success: function(ret, err) {
					console.log(JSON.stringify(err))
					if (ret) {
						if (ret.status == 200) {
							if (callback) callback(ret.data);
						} else {
							vm.showLoading = false;
							vm.refreshLoading = false;
							vm.ifConfirm = false;
							if (cmd != 'triggerActivity') {
								vm.$toast(ret.msg);
							}

						}
					} else {
						vm.showLoading = false;
						vm.refreshLoading = false;
						vm.ifConfirm = false;
						vm.$toast('程序好像出错了,请稍后再试');
					}
				}
			});
		},

		shoppingMallAjax(cmd, param, callback) {
			var url = 'http://' + FLD.shoppingMallServerIP + '/Handler/ShoppingHandler.ashx?action=' + cmd;
			if (FLD.getUserInfo()) {
				if (!param.hasOwnProperty("h_userId")) {
					param.h_userId = FLD.getUserInfo().UserId;
				}
			}
			$.ajax({
				url: url,
				async: true,
				data: param,
				type: "post",
				dataType: 'json',
				success: function(ret) {
					if (ret) {
						callback(ret)
					} else {
						vm.showLoading = false;
						vm.refreshLoading = false;
						vm.ifConfirm = false;
						vm.$toast('程序好像出错了,请稍后再试');
					}
				}
			});
		},

		logisticsAjax(param, callback) {
			var url = 'http://' + FLD.shoppingMallServerIP + '/API/VshopProcess.ashx?action=Logistic';
			if (FLD.getUserInfo()) {
				if (!param.hasOwnProperty("h_userId")) {
					param.h_userId = FLD.getUserInfo().UserId;
				}
			}
			api.ajax({
				url: url,
				method: 'post',
				dataType: 'json',
				data: {
					values: param
				}
			}, function(ret, err) {
				if (ret) {
					callback(ret)
				} else {
					vm.showLoading = false;
					vm.refreshLoading = false;
					vm.ifConfirm = false;
					vm.$toast('程序好像出错了,请稍后再试');
				}


			});
		},

		nativeRequest_shopPay(cmd, param, callback) {
			var url = 'http://' + FLD.serverIP + '/Frame/ST1_FLD/SP_PayHandler.aspx?cmd=' + cmd;
			if (FLD.getUserInfo()) {
				if (!param.hasOwnProperty("h_userId")) {
					param.h_userId = FLD.getUserInfo().UserId;
				}
			}
			api.ajax({
				url: url,
				method: 'post',
				dataType: 'text',
				data: {
					values: param
				}
			}, function(ret, err) {
				if (ret) {
					callback(ret)
				} else {
					console.log(JSON.stringify(err))
					vm.showLoading = false;
					vm.refreshLoading = false;
					vm.ifConfirm = false;
					vm.$toast('程序好像出错了,请稍后再试');
				}


			});
		},

		nativeRequest_moneyPay(cmd, param, callback) {
			var url = 'http://' + FLD.serverIP + '/Frame/ST1_FLD/SP_BalanceHandler.aspx?cmd=' + cmd;
			if (FLD.getUserInfo()) {
				if (!param.hasOwnProperty("h_userId")) {
					param.h_userId = FLD.getUserInfo().UserId;
				}
			}
			api.ajax({
				url: url,
				method: 'post',
				dataType: 'text',
				data: {
					values: param
				}
			}, function(ret, err) {
				if (ret) {
					callback(ret)
				} else {
					vm.showLoading = false;
					vm.refreshLoading = false;
					vm.ifConfirm = false;
					vm.$toast('程序好像出错了,请稍后再试');
				}


			});
		},


		nativeRequest_vin(cmd, param, callback) {
			var url = 'http://' + FLD.serverIP + '/Frame/ST1_FLD/VinHandler.aspx?cmd=' + cmd;
			if (FLD.getUserInfo()) {
				param.P_UserID = FLD.getUserInfo().UserID;
				param.userId = FLD.getUserInfo().UserID;
			} else {
				param.P_UserID = 0;
			}
			// console.log(JSON.stringify(url))
			// console.log(JSON.stringify(param))

			$.ajax({
				headers: {
					'Version': FLD.version,
					'PackageName': 'com.fldsc.app'
				},
				url: url,
				async: true,
				data: param,
				type: "post",
				dataType: 'json',
				function(ret, err) {
					// alert(JSON.stringify(ret))
					if (ret) {
						var json_ret = JSON.parse(ret);
						if (callback) callback(json_ret);
					} else {
						// alert(JSON.stringify(err) + JSON.stringify(param))
						if (cmd == 'SearchBusinessByVin' || cmd == 'SearchBusinessByOE' || cmd ==
							'SearchBusinessByPartsno' || cmd == 'SearchBusinessByFmsi' || cmd ==
							'SearchBusinessByCarBrand') {
							layer.open({
								content: '未查询到数据,请确认查询内容!',
								skin: 'msg',
								time: 3
							});


						} else {
							vm.$toast('程序好像出错了,请稍后再试\n' + 'errcode:' + (err ? err.code : '999'));
						}
						vm.showLoading = false;
						vm.refreshLoading = false;
						vm.ifConfirm = false;

					}
				}
			});

		},

		uploadFile(folder, fileSrc, callback) {
			var url = 'http://' + FLD.serverIP + '/Frame/uploadFile?dirName=' + folder;
			api.ajax({
				headers: {
					'Version': FLD.version,
					'PackageName': 'com.fldsc.app'
				},
				url: url,
				method: 'post',
				data: {
					files: {
						file: fileSrc
					}
				}
			}, function(ret, err) {
				if (ret) {
					if (ret.status && ret.status == 200) {
						if (callback) callback(ret.data);
					} else {
						if (vm) {
							vm.showLoading = false;
							vm.refreshLoading = false;
							vm.$toast('上传失败,请稍后再试');
						}

					}
				} else {
					if (vm) {
						vm.showLoading = false;
						vm.refreshLoading = false;
						vm.$toast('程序好像出错了,请稍后再试\n' + 'errcode:' + err.code);
					}
				}
			});
		},

		mustLogin(callback) {
			var userInfo = FLD.getUserInfo();
			if (!userInfo) {
				api.openWin({
					name: 'login',
					url: '../login.html',
					animation: {
						type: "movein", //动画类型（详见动画类型常量）
						subType: "from_bottom", //动画子类型（详见动画子类型常量）
						duration: 300 //动画过渡时间，默认300毫秒
					},
					reload: true,
					slidBackEnabled: false
				})
			} else {
				callback();
			}
		},

		exitApp() {
			setTimeout(function() {
				api.addEventListener({
					name: 'keyback'
				}, function(ret, err) {
					api.toast({
						msg: '再按一次返回键退出' + api.appName,
						duration: 2000,
						location: 'bottom'
					});
					api.addEventListener({
						name: 'keyback'
					}, function(ret, err) {
						api.closeWidget({
							id: 'A6036385458297',
							retData: {
								name: 'closeWidget'
							},
							silent: true
						});
					});
					setTimeout(function() {
						FLD.exitApp();
					}, 2000)
				});
			}, 400);

		},

		getFormatDate(date) {
			if (!date) date = new Date();
			var seperator1 = "-";
			var seperator2 = ":";
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			var hour = date.getHours();
			var minute = date.getMinutes();
			var second = date.getSeconds();
			if (month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if (strDate >= 0 && strDate <= 9) {
				strDate = "0" + strDate;
			}
			if (hour >= 0 && hour <= 9) {
				hour = "0" + hour;
			}
			if (minute >= 0 && minute <= 9) {
				minute = "0" + minute;
			}
			if (second >= 0 && second <= 9) {
				second = "0" + second;
			}
			var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
				" " + hour + seperator2 + minute +
				seperator2 + second;
			return currentdate;
		},

		getChatFormatDate(date) {
			if (!date) date = new Date();
			var seperator1 = "-";
			var seperator2 = ":";
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			var hour = date.getHours();
			var minute = date.getMinutes();
			if (month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if (strDate >= 0 && strDate <= 9) {
				strDate = "0" + strDate;
			}
			if (hour >= 0 && hour <= 9) {
				hour = "0" + hour;
			}
			if (minute >= 0 && minute <= 9) {
				minute = "0" + minute;
			}
			var currentdate = month + seperator1 + strDate +
				" " + hour + seperator2 + minute;
			return currentdate;
		},

		getUserInfo(data) {
			if (data) {
				window.localStorage.setItem('userInfo', data)
				// return null;
			} else {
				return JSON.parse(window.localStorage.getItem('userInfo'))
			}
		}
	}
}()
if (Vue) {

	Vue.component('serve-img1', {
		props: ['src'],
		template: '<img :src="returnServeImg(src)" />',
		methods: {
			returnServeImg(src) {
				if (src && src != 'null') {
					return 'http://' + FLD.serverIP + src;
				} else {
					return '../../image/header.png';
				}

			}
		}
	})
	Vue.component('serve-img2', {
		props: ['src'],
		template: '<img :src="returnServeImg(src)" />',
		methods: {
			returnServeImg(src) {
				if (src) {
					return 'http://' + FLD.serverIP + src;
				} else {
					return '../../image/no.png';
				}

			}
		}
	})
}

var systemType = window.localStorage.getItem('systemType');
var appTop = parseInt(window.localStorage.getItem('safeAreaTop'));
var appBottom = parseInt(window.localStorage.getItem('safeAreaBottom'));

$(function() {
	if (appTop) {
		$('.head').css({
			'padding-top': appTop + 'px',
			'height': appTop + 40 + 'px',
			'line-height': '40px'
		})
	} else {
		$('.head').css({
			'padding-top': appTop + 25 + 'px',
			'height': '65px',
			'line-height': '40px'
		})
	}
	// $('.body-no-bottom').css({
	//     'height': 'calc(100% - ' + (appTop + 40) + 'px)'
	// })
	// $('.body-with-bottom').css({
	//     'height': 'calc(100% - ' + (appTop + 90) + 'px)'
	// })

	//vant下拉刷新与向上滚动的冲突问题解决
	setTimeout(function() {
		if (typeof(vm) != 'undefined') {
			if (vm.loadingDisabled != undefined) {
				$('.content').scroll(function() {
					if ($(this).scrollTop() == 0) {
						vm.loadingDisabled = false;
					} else {
						vm.loadingDisabled = true;
					}
				})
			}
		}
	}, 200)
	//vue移动端点击方法300毫秒延迟问题解决
	if (FastClick) {
		FastClick.attach(document.body)
	}
})